﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using DoMoreWithVision.VisionObjects;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using System.Web;
using System.Web.Configuration;
using Microsoft.Win32;

namespace DoMoreWithVision.ConfigureWCFTimesheet
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string strConfigFile = "";

        Dictionary<string, string> rules = new Dictionary<string, string>();

        public MainWindow()
        {
            InitializeComponent();
            //testc();
            InitDropDowns();
        }

        private void InitDropDowns()
        { 
            Project p = new Project();
            Day d = new Day();
            TKMaster tkm = new TKMaster();
            TKDetail tkd = new TKDetail();

            cmbProject.ItemsSource = p.GetType().GetProperties();
            cmbProject.DisplayMemberPath = "Name";
            cmbProject.SelectedValuePath = "Name";
            
            cmbDays.ItemsSource = d.GetType().GetProperties();
            cmbDays.DisplayMemberPath = "Name";
            cmbDays.SelectedValuePath = "Name";

            cmbTkDetail.ItemsSource = tkm.GetType().GetProperties();
            cmbTkDetail.DisplayMemberPath = "Name";
            cmbTkDetail.SelectedValuePath = "Name";

            cmbTkMaster.ItemsSource = tkd.GetType().GetProperties();
            cmbTkMaster.DisplayMemberPath = "Name";
            cmbTkMaster.SelectedValuePath = "Name";


        }

        //private void UpdateConfig(string key, string value, string fileName)
        //{
        //    var configFile = ConfigurationManager.OpenExeConfiguration(fileName);
        //    configFile.AppSettings.Settings[key].Value = value;
        //    var list = configFile.AppSettings.Settings["ErrorsCheck"];

        //    configFile.Save();
        //}

      

        //private void testc()
        //    {
        //        ValidateTest.ValidateTimesheetClient test = new ValidateTest.ValidateTimesheetClient();
        //    string testres=    test.ValidateTimesheetFunc("test");
        //    MessageBox.Show(testres);
        //}

        private void btnAddDay_Click(object sender, RoutedEventArgs e)
        {
            txtRule.Text = txtRule.Text.ToString() + " " + cmbDays.SelectedValue.ToString();
        }

        private void btnAddTkmaster_Click(object sender, RoutedEventArgs e)
        {
        
            txtRule.Text = txtRule.Text.ToString() + " " + cmbTkMaster.SelectedValue.ToString();
        }

        private void btnAddProject_Click(object sender, RoutedEventArgs e)
        {
            txtRule.Text = txtRule.Text.ToString() + " " + cmbProject.SelectedValue.ToString();
        }

        private void btnAddTkdetail_Click(object sender, RoutedEventArgs e)
        {
            txtRule.Text = txtRule.Text.ToString() + " " + cmbTkDetail.SelectedValue.ToString();
        }

        private void btnAddQuote_Click(object sender, RoutedEventArgs e)
        {
            GetConfig("C:\\Users\\JJ\\Desktop\\td\\Visual Studio 2013\\Projects\\Vision\\WCFTimesheetService\\TimesheetWebService\\TimesheetWebService\\web.config");

        }

        private void btnAddEq_Click(object sender, RoutedEventArgs e)
        {
            txtRule.Text = txtRule.Text.ToString() + " == ";
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
                       // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "Text Files (.config)|*.config|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.InitialDirectory = "C:\\Users\\JJ\\Desktop\\td\\Visual Studio 2013\\Projects\\Vision\\WCFTimesheetService\\TimesheetWebService\\TimesheetWebService\\";

            openFileDialog1.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == true)
            {
                // Open the selected file to read.
                strConfigFile = openFileDialog1.FileName;
                GetConfig(strConfigFile);
            }
        }

        private void GetConfig(string fileName)
        {
            // var configFile = ConfigurationManager.OpenExeConfiguration(fileName);

            // config = XDocument.Load(fileName);
            XmlDocument config = new XmlDocument();
            config.Load(fileName);
            var node = config.SelectNodes("//configuration//applicationSettings//TimesheetWebService.Properties.Settings//setting//ArrayOfString");

            rules.Clear();

            foreach (XmlNode item in node[0].ChildNodes)
            {
                string value = item.InnerText;
                //"<errors><error>This is my first timesheet error</error></errors>";
                string error = value.Substring(value.IndexOf("<errors><error>") + 15, ((value.IndexOf("</errors>") - 8) - (value.IndexOf("<errors><error>") + 15)));
                string rule = value.Substring(0, value.IndexOf("<errors><error>"));


                rules.Add(rule, error);

                            }
            cmbRules.ItemsSource = rules;
            cmbRules.DisplayMemberPath = "Key";
            cmbRules.SelectedValuePath = "Key";

        }

        private void cmbRules_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbRules.SelectedValue == null)
            {
                txtError.Text = "";
                txtRule.Text = "";
            }
            else if (rules.ContainsKey(cmbRules.SelectedValue.ToString()))
            {
                txtError.Text = rules[cmbRules.SelectedValue.ToString()];
                txtRule.Text = cmbRules.SelectedValue.ToString();
            }
            else
            {
                txtError.Text = "";
                txtRule.Text = "";

            }
        }

        private void btnUpdateRule_Click(object sender, RoutedEventArgs e)
        {
            rules.Remove(cmbRules.SelectedValue.ToString());

            rules.Add(txtRule.Text, txtError.Text);
            cmbRules.Items.Refresh();
        }

        private void btnRemoveRule_Click(object sender, RoutedEventArgs e)
        {
            rules.Remove(cmbRules.SelectedValue.ToString());
            cmbRules.Items.Refresh();
        }

        private void btnAddRule_Click(object sender, RoutedEventArgs e)
        {
            if (rules.ContainsKey(txtRule.Text))
            {
                if (rules.ContainsKey("") == false)
                {
                    rules.Add("", "");
                    cmbRules.Items.Refresh();
                    cmbRules.SelectedValue = "";
                    txtError.Text = "";
                    txtRule.Text = "";
                }
            }
            else
            {
                rules.Add(txtRule.Text, txtError.Text);
                cmbRules.Items.Refresh();
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            XmlDocument config = new XmlDocument();
            config.Load(strConfigFile);
            var node = config.SelectNodes("//configuration//applicationSettings//TimesheetWebService.Properties.Settings//setting//ArrayOfString");

            //remove old entries
            node[0].RemoveAll();

            foreach (var item in rules)
            {
                XmlNode rule = config.CreateElement("string");
                rule.InnerText = item.Key + "<errors><error>" + item.Value.ToString() + "</error></errors>";
                node[0].AppendChild(rule);
            }

            config.Save(strConfigFile + ".bak");

        }

        private void btnAddNotEq_Click(object sender, RoutedEventArgs e)
        {

            txtRule.Text = txtRule.Text.ToString() + " != ";
        }

        private void btnAddLess_Click(object sender, RoutedEventArgs e)
        {

            txtRule.Text = txtRule.Text.ToString() + " < ";
        }

        private void btnAddGreat_Click(object sender, RoutedEventArgs e)
        {

            txtRule.Text = txtRule.Text.ToString() + " > ";
        }

        private void btnAddOR_Click(object sender, RoutedEventArgs e)
        {

            txtRule.Text = txtRule.Text.ToString() + " || ";
        }

        private void btnAddAND_Click(object sender, RoutedEventArgs e)
        {

            txtRule.Text = txtRule.Text.ToString() + " && ";
        }
    }
}
